import Vue from 'vue'

import appurl_config from './config'
const BaseUrl = appurl_config.appurl;

class Request {
	constructor() {}
	getWithToken(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"GET",
				header:{
					'token': uni.getStorageSync('userToken') ? uni.getStorageSync('userToken') : '',
					'X-Tenant-Id': appurl_config.tenantId,
					'X-App-Type': appurl_config.appType
				},
				success(res){
					console.log(res)
					if(res.data.code == 401){
						console.log('未登录')
						resolve(res.data);
						uni.navigateTo({
							url: '/pages/register/index'
						});
						return;
					}
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	postWithToken(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"POST",
				header:{
					'token': uni.getStorageSync('userToken') ? uni.getStorageSync('userToken') : '',
					'X-Tenant-Id': appurl_config.tenantId,
					'X-App-Type': appurl_config.appType
					// 'token':Vue.prototype.Token ? Vue.prototype.Token : ''
				},
				dataType: 'json',
				success(res){
					if(res.data.code == 401){
						uni.navigateTo({
							url: '/pages/register/index'
						});
						return;
					}
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	get(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"GET",
				header:{
					'X-Tenant-Id': appurl_config.tenantId,
					'X-App-Type': appurl_config.appType
				},
				success(res){
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
	post(obj){
		return new Promise((resolve, reject) => {
			uni.request({
				url:BaseUrl+obj.url,
				data:obj.data,
				method:"POST",
				header:{
					'X-Tenant-Id': appurl_config.tenantId,
					'X-App-Type': appurl_config.appType
				},
				success(res){
					resolve(res.data);
				},
				fail(res){
					reject(res.data);
				}
			})
		})
	}
}
export const request = new Request()